﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeAngel.Domain.Framework
{
    public static class Extensions
    {
        /// <summary>
        /// Finds the DTO with the identified id in a list of IDto objects.  Null is returned
        /// if a match is not found.
        /// </summary>
        //public static IDto FindByID1(this List<IDto> theseDtos, Guid id)
        //{
        //    return theseDtos.FirstOrDefault(ThisDto => ThisDto.ID == id);
        //}


        public static T Find<T>(this List<T> theseDtos, Guid id) where T : class, IDto
        {
            if (theseDtos == null) return null;
            foreach (T thisDto in theseDtos)
                if (thisDto.ID.Equals(id)) return thisDto;
            return null;
            //return theseDtos.Find(dto => dto.ID.Equals(id));
        }

        public static T Find<T>(this List<T> theseDtos, Guid? id) where T : class, IDto
        {
            if (theseDtos == null || !id.HasValue ) return null;
            //return theseDtos.Find(dto => dto.ID.Equals(id.Value));
            return theseDtos.Find(id.Value);
        }

        public static List<T> FindAll<T>(this List<T> items, Predicate<T> condition)
        {
            var MatchingItems = new List<T>();
            foreach (T thisItem in items)
                if (condition(thisItem)) MatchingItems.Add(thisItem);
            return MatchingItems;
            //return theseDtos.Find(dto => dto.ID.Equals(id));
        }


    }
}
